package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ProjectPlanRequestDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.ContractVo;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
@Autowired
NursingProjectMapper nursingProjectMapper;
@Autowired
NursingProjectPlanMapper nursingProjectPlanMapper;
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> nursingProjects = (Page<NursingProjectVo>) nursingProjectMapper.selectByPage(name, status);
        PageResponse<NursingProjectVo> projectVoPageResponse = PageResponse.of(nursingProjects, NursingProjectVo.class);
        return projectVoPageResponse;
    }

    @Override
    public void addNursingProject(NursingProjectVo nursingProjectVo) {
        //校验名称是否重名
        NursingProject nursingProject = nursingProjectMapper.getByName(nursingProjectVo.getName());
        if (ObjectUtil.isNotEmpty(nursingProject)) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_ADD_FAIL_RENAME);
        }

        nursingProjectMapper.addNursingProject(nursingProjectVo);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        //判断id是否存在
        NursingProjectVo nursingProjectVo = nursingProjectMapper.getById(id);
        if (ObjectUtil.isEmpty(nursingProjectVo)) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_SELECT_NULL);
        }
        return nursingProjectVo;
    }

    @Override
    public void updateNursingProject(NursingProjectVo nursingProjectVo) {
        //若该项目已与护理计划进行绑定删除编辑禁用按钮置灰，不可点击；2.重名问题

        //1.查询护理计划表与项目关系表，去查他的id如果能查到，则提示项目已与护理计划绑定，不可删除或编辑
//        int ggg = nursingProjectPlanMapper.slectById(nursingProjectVo.getId());
//        if (ggg > 0) {
//            throw new BaseException(BasicEnum.NURSING_PROJECT_UPDATE_FAIL_RENAME);
//        }

        //重名问题
        NursingProject nursingProject = nursingProjectMapper.bbb(nursingProjectVo);
        if (ObjectUtil.isNotEmpty(nursingProject)) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_BIND);
        }


        nursingProjectMapper.updateNursingProject(nursingProjectVo);
    }

    @Override
    public void ooo(long id, Integer status) {
        if (status == 0) {
            //禁用
            //判断项目是否被关联，如果被关联，则提示项目已与护理计划绑定，不可删除或编辑
            int count = nursingProjectPlanMapper.slectById(id);
            if (count > 0) {
                throw new BaseException(BasicEnum.NURSING_PROJECT_BIND);
            }
        }
        nursingProjectMapper.ooo( id, status);
    }

    @Override
    public void deleteById(Long id) {
//        //若该项目已与护理计划进行绑定，删除、编辑、禁用按钮置灰，不可点击；
//         NursingProjectVo uu  = getById(id);
//         if(uu.getCount()>0){
//             throw new BaseException(BasicEnum.NURSING_DELETE_FAIL);
//         }
        int ggg = nursingProjectPlanMapper.slectById(id);
        if (ggg > 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_BIND);
        }
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> getAll() {
        return nursingProjectMapper.getAll();
    }

//    @Override
//    public List<NursingPlanVo> getAllPlan() {
//        return nursingProjectMapper.getAllPlan();
//    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void addNursingPlan(ProjectPlanRequestDto projectPlanRequestDto) {
//        //这边要插入两张表1第一张是计划表，是有一张是他和项目的关联表，根据传入的id来给关联表中插入一条或多条数据
//        nursingProjectMapper.addNursingPlan(projectPlanRequestDto);
//        for (ProjectPlanRequestDto.ProjectPlan projectPlan : projectPlanRequestDto.getProjectPlans()) {
////            projectPlan.setCreateTime(LocalDateTime.now());
////            projectPlan.setUpdateTime(LocalDateTime.now());
////            projectPlan.setCreateBy(1L);
////            projectPlan.setUpdateBy(1L);
//            nursingProjectPlanMapper.addNursingProjectPlan(projectPlan);
//        }
//    }
//
//    @Override
//    public PageResponse<NursingPlanVo> getByPagePlan(String name, Integer status, Integer pageNum, Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        Page<NursingPlanVo> nursingProjects = (Page<NursingPlanVo>) nursingProjectMapper.selectByPagePlan(name, status);
//        PageResponse<NursingPlanVo> projectVoPageResponse = PageResponse.of(nursingProjects, NursingPlanVo.class);
//        return projectVoPageResponse;
//    }
}
